package org.jeecg.modules.MedicalInsuranceBigScreen;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.sggg.entity.SgMember;
import org.jeecg.modules.sggg.service.ISgBlutdruckRecordService;
import org.jeecg.modules.sggg.service.ISgGlucoseRecordService;
import org.jeecg.modules.sggg.service.ISgLipidRecordService;
import org.jeecg.modules.sggg.service.ISgMemberService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 医保大屏-检测指导大屏
 */
@RestController
@RequestMapping("/DetectGuide")
@Slf4j
public class DetectGuideController {

    @Autowired
    private ISgMemberService sgMemberService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISgBlutdruckRecordService sgBlutdruckRecordService;

    @Autowired
    private ISgGlucoseRecordService sgGlucoseRecordService;

    @Autowired
    private ISgLipidRecordService sgLipidRecordService;

    /**
     * 高危干预预警
     *
     * @param departName 机构名称
     * @return 高危干预预警数据
     */
    @GetMapping("/getHighRiskWarning")
    public Result<List<Map<String, Object>>> getHighRiskWarning(String departName) {
        String departCode = null;
        SysDepart depart = new SysDepart();
        if (!Objects.equals(departName, "扎赉特旗")) {
            depart = sysDepartService.getOne(
                    new QueryWrapper<SysDepart>()
                            .like("depart_name", departName)
            );
        }
        if (depart != null) {
            departCode = depart.getOrgCode();
        }
        List<Map<String, Object>> list = sgMemberService.getHighRiskWarning(departCode);
        return Result.OK(list);
    }

    /**
     * 三高人数统计
     *
     * @param sysUser
     * @return
     */
    @GetMapping("/threeHighStatistics")
    public Result<JSONObject> threeHighStatistics(SysUser sysUser) {
        JSONObject jsonObject = new JSONObject();

        // 血压
        QueryWrapper<SgMember> memberQueryWrapperBlutdruck = new QueryWrapper<>();
        // 血糖
        QueryWrapper<SgMember> memberQueryWrapperGlucose = new QueryWrapper<>();
        // 血脂
        QueryWrapper<SgMember> memberQueryWrapperLipid = new QueryWrapper<>();

        memberQueryWrapperBlutdruck
                .like("three_tag", 0)
                .eq("del_flag", 0)
                .likeLeft("sys_org_code", sysUser.getOrgCode());
        memberQueryWrapperGlucose
                .like("three_tag", 1)
                .eq("del_flag", 0)
                .like("sys_org_code", sysUser.getOrgCode());
        memberQueryWrapperLipid
                .like("three_tag", 2)
                .eq("del_flag", 0)
                .like("sys_org_code", sysUser.getOrgCode());

        jsonObject.put("blutdruck", sgMemberService.list(memberQueryWrapperBlutdruck).size());
        jsonObject.put("glucose", sgMemberService.list(memberQueryWrapperGlucose).size());
        jsonObject.put("lipid", sgMemberService.list(memberQueryWrapperLipid).size());
        return Result.ok(jsonObject);
    }

    /**
     * 获取慢病管理人数
     *
     * @param sysUser
     * @return
     */
    @GetMapping("/getChronicMamtPatientCount")
    public Result<List<Map<String, Integer>>> getChronicMamtPatientCount(SysUser sysUser) {
        List<Map<String, Integer>> map = new ArrayList<>();

        if (sysUser.getType().equals(1)) {
            map = sgBlutdruckRecordService.getEachLevelNum(sysUser.getOrgCode());
        } else if (sysUser.getType().equals(2)) {
            map = sgGlucoseRecordService.getEachLevelNum(sysUser.getOrgCode());
        } else if (sysUser.getType().equals(3)) {
            map = sgLipidRecordService.getEachLevelNum(sysUser.getOrgCode());
        }

        return Result.OK(map);
    }
}
